#!/usr/bin/env groovy

pipeline {
    agent { label 'jenkins-workspace' }
    parameters {
        booleanParam(name: 'CERTIFY_SAFE', defaultValue: false, description: 'I have reviewed *the latest version of* these changes and I am sure that they don’t contain any code that could compromise the security of the CI infrastructure.')
        string(name: 'PR_ID', defaultValue: '', description: 'PR ID')
    }
    stages {
        stage("Setup repository") {
            steps {
                checkout(
                    changelog: false,
                    poll: false,
                    scm: [
                        $class: 'GitSCM',
                        branches: [[
                            name: 'refs/heads/_jenkins_local_branch'
                        ]],
                        userRemoteConfigs: [[
                            credentialsId: "96d5f81c-e9ad-45f7-ba5d-bc8107c0ae2c",
                            url: "git@github.com:nodejs/node.git"
                        ]]
                    ]
                )
            }
        }

        stage('Preflight') {
            steps {
                script {
                    if (!params.CERTIFY_SAFE) {
                        error "Please certify that you have reviewed the changes to be tested, by ticking the CERTIFY_SAFE checkbox on the job launch page."
                    }
                    setupEnv(env, params)
                }
            }
        }

        stage('Run tests') {
            steps {
                script {
                    def buildParams = setupSubParams(env, params)
                    def testResult
                    parallel(
                        regressionTests: {
                            testResult = build job: "node-test-commit-linuxone", parameters: buildParams, propagate: false
                        },
                        linter: {
                            build job: "node-linter", parameters: buildParams, propagate: true
                        }
                    )
                    currentBuild.result = testResult.result
                }
            }
        }
    }
}

def getBranchName(params) {
    if (params.PR_ID) {
        return "refs/pull/${params.PR_ID}/head"
    } else {
        return "refs/heads/master"
    }
}

def setupEnv(env, params) {
    def node_version = sh(script: "python tools/getnodeversion.py", returnStdout: true).trim()
    echo "Detected node version: ${node_version}"
    def node_version_parts = node_version.tokenize('.')
    env.NODE_MAJOR_VERSION = node_version_parts[0]
    echo "NODE_MAJOR_VERSION=${env.NODE_MAJOR_VERSION}"
}

def setupSubParams(env, params) {
    def pr = [:] // Mutable copy of params.
    params.each{ key, value -> pr.put(key, value) }
    pr["GITHUB_ORG"] = "nodejs"
    pr["REPO_NAME"] = "node"
    pr["GIT_REMOTE_REF"] = getBranchName(params)
    pr["GIT_ORIGIN_SCHEME"] = "git@github.com:"
    pr['NODE_MAJOR_VERSION'] = env.NODE_MAJOR_VERSION

    p = []
    for (param in pr) {
        p.push(string(name: param.key, value: "${param.value}"))
    }
    return p
}
